package example.set;


import entity.Student;

import java.util.Set;
import java.util.TreeSet;

/**
 * 定制排序 ：实现Comparable接口，自行根据属性排序；
 */
public class TreeSetTest2 {
    public static void main(String[] args) {

        Set<Student> students = new TreeSet<>((o1, o2) -> {
            if (o1.getAge() == o2.getAge()) {
                return o1.getName().compareTo(o2.getName());
            } else {
                return o1.getAge() - o2.getAge();
            }
        });


        students.add(new Student(12,"aa"));
        students.add(new Student(8,"bb"));
        students.add(new Student(80,"cc"));
        students.add(new Student(17,"dd"));
        students.add(new Student(5,"ee"));
        students.add(new Student(22,"ff"));
        students.add(new Student(22,"jj"));

        for (Object obj : students) {
            System.out.println(obj);
        }
    }
}




